package com.souku.finance.application.ui.mine;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.android.volley.manager.RequestManager;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.souku.finance.application.R;
import com.souku.finance.application.base.BaseFragment;
import com.souku.finance.application.bean.UserAccMoreBean;
import com.souku.finance.application.http.UrlConstact;
import com.souku.finance.application.http.volley.ReqListener;
import com.souku.finance.application.http.volley.SuccessError;
import com.souku.finance.application.ui.invite.CalculationActivity;
import com.souku.finance.application.ui.login.LoginActivity;
import com.souku.finance.application.utils.Constant;
import com.souku.finance.application.utils.JsonUtil;
import com.souku.finance.application.utils.MyTextUtil;
import com.souku.finance.application.utils.ParamUtil;
import com.souku.finance.application.utils.ShareUtil;
import com.souku.finance.application.utils.ToastUtil;
import com.souku.finance.application.widget.CircleViewHomeNopoint;
import com.souku.finance.application.widget.MyRelativeLayout;
import com.souku.finance.application.widget.MyTextView;
import com.umeng.analytics.MobclickAgent;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by Android on 2016/12/5.
 */

public class MineFragment extends BaseFragment implements View.OnClickListener {

    protected View rootView;
    protected CircleViewHomeNopoint circle;
    protected TextView tvCircle;
    protected RelativeLayout mineRelative1;
    protected RelativeLayout mineRelative2;
    protected RelativeLayout mineRelative3;
    protected RelativeLayout mineRelative4;
    protected RelativeLayout mineRelative5;
    protected RelativeLayout mineRelative6;
    protected RelativeLayout mineFirstLibrary;
    protected RelativeLayout mineAccumulativeEarnings;
    protected PullToRefreshScrollView scrollview;
    protected TextView tvRecharge;
    protected TextView tvWithdraw;
    protected LinearLayout topTotal;
    protected RelativeLayout rlTopTotal;
    public static boolean isChange = true;
    protected MyTextView tvAccumulatedInterest;
    protected TextView tvToRepayCount;
    protected TextView tvMoney;
    protected TextView tvBmMoney;
    protected TextView tvExperienceMoney;
    protected TextView tvAvailablePromCount;
    protected TextView tvAvailablePoint;
    protected MyTextView tvToReceivedInterest;
    protected MyRelativeLayout relativeTotal;
    @BindView(R.id.tv_total_asset_money)
    TextView tvTotalAssetMoney;
    @BindView(R.id.tv_income_money)
    TextView tvIncomeMoney;
    @BindView(R.id.tv_income)
    TextView tvIncome;

    TextView tvMoneyAccumulativeEarnings;
    TextView tvAccount;
    TextView tvMoneyFirstLibrary;

    private UserAccMoreBean bean;

    Unbinder unbinder;

    //MobclickAgent.onEvent(MainActivity.this, "borrow");

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fra_mine, container, false);
        setContent(rootView);
        initView(rootView);
        circle.setStoke(getActivity());
        circle.setPrograss(0, 0, tvCircle);
        scrollview.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ScrollView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ScrollView> refreshView) {
                if (ShareUtil.getInstance().getBoolean(Constant.IsLogin)) {
                    getUserAccMore();
                } else {
                    scrollview.onRefreshComplete();
                }
            }
        });
        unbinder = ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        getUserAccMore();
    }

    @Override
    public void internet() {
//        if (ShareUtil.getInstance().getBoolean(Constant.IsLogin)) {
//            scrollview.setRefreshing(true);
//        }
    }

    @Override
    public void showFragment() {
//        if (scrollview.isRefreshing()) {
//            scrollview.onRefreshComplete();
//        }
//        if (bean == null && ShareUtil.getInstance().getBoolean(Constant.IsLogin)) {
//            scrollview.setRefreshing(true);
//        } else {
//            setData();
//        }
    }

    private void initView(View rootView) {
        circle = (CircleViewHomeNopoint) rootView.findViewById(R.id.circle);
        tvCircle = (TextView) rootView.findViewById(R.id.tv_circle);
        mineRelative1 = (RelativeLayout) rootView.findViewById(R.id.mine_relative1);
        mineRelative1.setOnClickListener(MineFragment.this);
        mineRelative2 = (RelativeLayout) rootView.findViewById(R.id.mine_relative2);
        mineRelative2.setOnClickListener(MineFragment.this);
        mineRelative3 = (RelativeLayout) rootView.findViewById(R.id.mine_relative3);
        mineRelative3.setOnClickListener(MineFragment.this);
        mineRelative4 = (RelativeLayout) rootView.findViewById(R.id.mine_relative4);
        mineRelative4.setOnClickListener(MineFragment.this);
        mineRelative5 = (RelativeLayout) rootView.findViewById(R.id.mine_relative5);
        mineRelative5.setOnClickListener(MineFragment.this);
        mineRelative6 = (RelativeLayout) rootView.findViewById(R.id.mine_relative6);
        mineRelative6.setOnClickListener(MineFragment.this);
        mineAccumulativeEarnings = (RelativeLayout) rootView.findViewById(R.id.mine_accumulative_earnings);
        mineAccumulativeEarnings.setOnClickListener(MineFragment.this);
        mineFirstLibrary = (RelativeLayout) rootView.findViewById(R.id.mine_first_library);
        mineFirstLibrary.setOnClickListener(MineFragment.this);
        scrollview = (PullToRefreshScrollView) rootView.findViewById(R.id.scrollview);
        tvRecharge = (TextView) rootView.findViewById(R.id.tv_recharge);
        tvRecharge.setOnClickListener(MineFragment.this);
        tvWithdraw = (TextView) rootView.findViewById(R.id.tv_withdraw);
        tvWithdraw.setOnClickListener(MineFragment.this);
        topTotal = (LinearLayout) rootView.findViewById(R.id.top_total);
        topTotal.setOnClickListener(MineFragment.this);
        rlTopTotal = (RelativeLayout) rootView.findViewById(R.id.rl_top_total);
        rlTopTotal.setOnClickListener(MineFragment.this);
        tvAccumulatedInterest = (MyTextView) rootView.findViewById(R.id.tv_accumulatedInterest);
//        tvToRepayCount = (TextView) rootView.findViewById(R.id.tv_toRepayCount);
        tvMoney = (TextView) rootView.findViewById(R.id.tv_money);
        tvBmMoney = (TextView) rootView.findViewById(R.id.tv_bmMoney);
        tvExperienceMoney = (TextView) rootView.findViewById(R.id.tv_experienceMoney);
//        tvAvailablePromCount = (TextView) rootView.findViewById(R.id.tv_availablePromCount);
        tvAvailablePoint = (TextView) rootView.findViewById(R.id.tv_availablePoint);
        tvToReceivedInterest = (MyTextView) rootView.findViewById(R.id.tv_toReceivedInterest);
        tvMoneyAccumulativeEarnings = (TextView) rootView.findViewById(R.id.tv_money_accumulative_earnings);
        relativeTotal = (MyRelativeLayout) rootView.findViewById(R.id.relative_total);
        tvAccount = (TextView) rootView.findViewById(R.id.tv_account);
        tvMoneyFirstLibrary = (TextView) rootView.findViewById(R.id.tv_money_first_library);
        tvAccount.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.mine_relative6) {
            ToastUtil.show("敬请期待");
        } else if (!ShareUtil.getInstance().getBoolean(Constant.IsLogin)) {
            Intent intent = new Intent(getActivity(), LoginActivity.class);
            startActivity(intent);
        } else if (view.getId() == R.id.mine_relative1) {
            MobclickAgent.onEvent(getActivity(), "mine_investment");//我的投资
            Intent intent = new Intent(getActivity(), InvestRecordActivity.class);//我的投资
            startActivity(intent);
        } else if (view.getId() == R.id.mine_relative2) {
            if (bean == null) {
                scrollview.setRefreshing(true);
            } else {
                Intent intent = new Intent(getActivity(), BalanceActivity.class);
                intent.putExtra(Constant.Default, bean.getMoney());
                startActivity(intent);
            }
        } else if (view.getId() == R.id.mine_relative3) {
            Intent intent = new Intent(getActivity(), CurrentActivity.class);//类似首库宝
            startActivity(intent);
        } else if (view.getId() == R.id.mine_relative4) {
            MobclickAgent.onEvent(getActivity(), "mine_experience");
            Intent intent = new Intent(getActivity(), ExperienceActivity.class);//体验金
            startActivity(intent);
        } else if (view.getId() == R.id.mine_relative5) {
            MobclickAgent.onEvent(getActivity(), "mine_wraparound");
            Intent intent = new Intent(getActivity(), DiscountActivity.class);//我的卷包
            startActivity(intent);
        } else if (view.getId() == R.id.tv_recharge) {
            MobclickAgent.onEvent(getActivity(), "mine_recharge");//充值
            String isBind = ShareUtil.getInstance().getString(Constant.IsBind);
            Intent intent = new Intent();
            if (isBind.equals("0")) {
                intent.setClass(getActivity(), EAccountActivity.class);//去绑定E账号
            } else {
                intent.setClass(getActivity(), RechargrActivity.class);//充值
            }
            startActivity(intent);
        } else if (view.getId() == R.id.tv_withdraw) {
            MobclickAgent.onEvent(getActivity(), "mine_withdraw");//提现
            String isBind = ShareUtil.getInstance().getString(Constant.IsBind);
            Intent intent = new Intent();
            if (isBind.equals("0")) {
                intent.setClass(getActivity(), EAccountActivity.class);//去绑定E账号
            } else {
                intent.setClass(getActivity(), WithDrawActivity.class);//提现
            }
            startActivity(intent);
        } else if (view.getId() == R.id.top_total || view.getId() == R.id.rl_top_total) {
            Intent intent = new Intent(getActivity(), TotalActivity.class);
            startActivity(intent);
        } else if (view.getId() == R.id.mine_accumulative_earnings) {

        } else if (view.getId() == R.id.mine_first_library) {
            MobclickAgent.onEvent(getActivity(), "mine_shoukubao");
            Intent intent = new Intent(getActivity(), ShouKuDetailsActivity.class);//首库宝
            startActivity(intent);
        } else if (view.getId() == R.id.tv_account) {
            MobclickAgent.onEvent(getActivity(), "mine_eaccount");//E账号
            Intent intent = new Intent();
            String isBind = ShareUtil.getInstance().getString(Constant.IsBind);
            if (isBind.equals("0")) {
                intent.setClass(getActivity(), EAccountActivity.class);//去绑定E账号
            } else {
                intent.setClass(getActivity(), EAccountYetBindingActivity.class);//已经E账号
            }
            startActivity(intent);
        }
    }


    private void getUserAccMore() {
        isChange = false;
        HashMap<String, String> map = ParamUtil.getParams();
        RequestManager.getInstance().post(UrlConstact.USER_ACC_MORE, ParamUtil.getSing(map), new ReqListener(getActivity(), new SuccessError() {
            @Override
            public void onSuccess(String response) {
                scrollview.onRefreshComplete();
                if (JsonUtil.getCode(response) == 1) {
                    bean = JsonUtil.fastBean(JsonUtil.getData(response).toString(), UserAccMoreBean.class);
                    setData();
                } else {
                    ToastUtil.show(JsonUtil.getMessage(response));
                }
            }

            @Override
            public void onError(String errorMsg) {
                ToastUtil.show("网络错误");
                scrollview.onRefreshComplete();
            }
        }), ParamUtil.getHeaders());
    }

    private void setData() {
        if (bean == null) {
//            tvAccumulatedInterest.setText("0.00");
//            tvToReceivedInterest.setText("0.00");

            tvMoney.setText("0.00");//账户余额
            tvExperienceMoney.setText("0.00");//体验金
            tvTotalAssetMoney.setText("0.00");//总资产
            tvIncomeMoney.setText("0.00");//待收收益
            tvMoneyAccumulativeEarnings.setText("0.00");//累计收益
            tvIncome.setText("0.00");//待收本金
            tvMoneyFirstLibrary.setText("0.00");//首库宝

//            tvBmMoney.setText("0.00");
//            tvAvailablePoint.setText("0");
//            tvAvailablePromCount.setText("0");
//            tvToRepayCount.setText("0 笔未回款");
//            circle.setPrograss(00.00, 00.00, tvCircle);

        } else {

//            tvAccumulatedInterest.setText(MyTextUtil.setMoneyToElement(bean.getAccumulatedInterest()));
//            tvToReceivedInterest.setText(MyTextUtil.setMoneyToElement(bean.getToReceivedInterest()));

            tvMoney.setText(MyTextUtil.setMoneyToElement(bean.getMoney()));//账户余额
            tvExperienceMoney.setText(MyTextUtil.setMoneyToElement(bean.getExperienceMoney()));//体验金
            tvTotalAssetMoney.setText(MyTextUtil.setMoneyToElement(bean.getTotalMoney()));//总资产
            tvIncomeMoney.setText(MyTextUtil.setMoneyToElement(bean.getToReceivedInterest()));//待收收益
            tvMoneyAccumulativeEarnings.setText(MyTextUtil.setMoneyToElement(bean.getAccumulatedInterest()));//累计收益
            tvIncome.setText(MyTextUtil.setMoneyToElement(bean.getToCollectedPrincipal()));//待收本金
            tvMoneyFirstLibrary.setText(MyTextUtil.setMoneyToElement(bean.getBmMoney()));//首库宝

//            tvBmMoney.setText(MyTextUtil.setMoneyToElement(bean.getBmMoney()));//活期产品
//            tvAvailablePoint.setText(bean.getAvailablePoint());//积分挖宝
//            tvAvailablePromCount.setText(bean.getAvailablePromCount());
//            tvToRepayCount.setText(bean.getToRepayCount() + " 笔未回款");
//            double total = Double.parseDouble(MyTextUtil.setMoneyToElement(bean.getTotalMoney()));
//            circle.setPrograss(total, total, tvCircle);

        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
